/**
 * Qiangungun.com Inc.
 * Copyright (c) 2015-2015 All Rights Reserved.
 */
package com.qiangungun.report.biz.service.action.query;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.qiangungun.core.enums.BizCode;
import com.qiangungun.report.biz.service.action.AbstractAction;
import com.qiangungun.report.biz.share.ActionContext;
import com.qiangungun.report.common.model.exceptions.RptServiceException;
import com.qiangungun.report.dal.dataobject.RptcfgDefineDO;
import com.qiangungun.report.service.model.ServiceRequest;
import com.qiangungun.report.service.model.ServiceResult;
import com.qiangungun.report.service.model.config.RptcfgDefine;

/**
 * 
 * @author hangdahong
 * @version $Id: QueryDefineFromCacheAction.java, v 0.1 2015年3月6日 下午4:17:07 hangdahong Exp $
 */
public class QueryDefineFromCacheAction extends AbstractAction{
    private static final Logger logger = LoggerFactory.getLogger(QueryDefineFromCacheAction.class);
    /** 
     * @see com.qiangungun.report.biz.share.BaseAction#perform()
     */
    @Override
    public void perform() throws RptServiceException {
        logger.info("开始从缓存中查询报表定义配置");
        ActionContext context=ActionContext.getContext();
        ServiceResult<RptcfgDefine> resp =(ServiceResult<RptcfgDefine>)context.getResponse();
        ServiceRequest<Long> req=(ServiceRequest<Long>)context.getRequest();
        Long defineId=req.getBody();
        
        if(defineId==null){
            logger.error("报表定义ID不能为空");
            throw new RptServiceException(BizCode.ParamIsNull,"报表定义ID不能为空");
        }
        
        RptcfgDefineDO rptcfgDefineDO= this.rptcfgDefineCache.get(defineId);
        
        if(rptcfgDefineDO==null){
            logger.error("缓存中没有报表定义配置");
            throw new RptServiceException(BizCode.BizDataNotExists,"缓存中没有报表定义配置");
        }
        
        RptcfgDefine cfgDefine=new RptcfgDefine();
        
        convert(rptcfgDefineDO,cfgDefine);
        resp.setCode(BizCode.Success);
        resp.setBody(cfgDefine);
        logger.info("从缓存中查询报表定义配置完毕:{}",cfgDefine);
    }
    
    private void convert(RptcfgDefineDO rptcfgDefineDO,RptcfgDefine cfgDefine){
        cfgDefine.setId(rptcfgDefineDO.getId());
        cfgDefine.setDefineCode(rptcfgDefineDO.getDefineCode());
        cfgDefine.setDefineName(rptcfgDefineDO.getDefineTitle());
        cfgDefine.setDefineTitle(rptcfgDefineDO.getDefineTitle());
        cfgDefine.setCreateTime(rptcfgDefineDO.getCreateTime());
        cfgDefine.setNote(rptcfgDefineDO.getNote());
        cfgDefine.setStatus(rptcfgDefineDO.getStatus());
    }

}
